
module diansha {
	export class shuzi {
		num: number;
		danwei: string
		private static arr: Array<string> = ["n", "k", "m", "b", "t", "aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh", "ii", "jj", "kk", "ll", "mm", "nn", "oo", "pp", "qq", "rr", "ss", "tt", "uu", "vv", "ww", "xx", "yy", "zz"];//31个
		private olddanwei: number;
		private newdanwei: number;
		private chadanwei: number;
		private oldi: number
		private newi: number
		private he: number;
		private arrs: Array<any>;
		public constructor(num?: number, danwei?: string) {
			if (num != null && danwei != null) {
				this.num = Number(num.toFixed(5));
				this.danwei = danwei;
			}

			this.arrs = new Array<any>()
			let ss = 4;
			for (let i = 0; i < diansha.shuzi.arr.length; i++) {
				this.arrs[i] = ss
				ss += 3;
			}
		}
		/**
		 * 判断长度，然后使用最合适的单位
		 */
		checklength(num: number): string {
			let s = num + "";
			if (s.indexOf(".") != -1) {
				s = s.substring(0, s.lastIndexOf("."));
			}
			//截至，获得整数位
			let length = s.length;
			if (length < this.arrs[0]) {
				return diansha.shuzi.arr[0]
			} else if (length < this.arrs[1]) {
				return diansha.shuzi.arr[1]
			} else if (length < this.arrs[2]) {
				return diansha.shuzi.arr[2]
			} else if (length < this.arrs[3]) {
				return diansha.shuzi.arr[3]
			} else if (length < this.arrs[4]) {
				return diansha.shuzi.arr[4]
			} else if (length < this.arrs[5]) {
				return diansha.shuzi.arr[5]
			} else if (length < this.arrs[6]) {
				return diansha.shuzi.arr[6]
			} else if (length < this.arrs[7]) {
				return diansha.shuzi.arr[7]
			} else if (length < this.arrs[8]) {
				return diansha.shuzi.arr[8]
			} else if (length < this.arrs[9]) {
				return diansha.shuzi.arr[9]
			} else if (length < this.arrs[10]) {
				return diansha.shuzi.arr[10]
			} else if (length < this.arrs[11]) {
				return diansha.shuzi.arr[11]
			} else if (length < this.arrs[12]) {
				return diansha.shuzi.arr[12]
			} else if (length < this.arrs[13]) {
				return diansha.shuzi.arr[13]
			} else if (length < this.arrs[14]) {
				return diansha.shuzi.arr[14]
			} else if (length < this.arrs[15]) {
				return diansha.shuzi.arr[15]
			} else if (length < this.arrs[16]) {
				return diansha.shuzi.arr[16]
			} else if (length < this.arrs[17]) {
				return diansha.shuzi.arr[17]
			} else if (length < this.arrs[18]) {
				return diansha.shuzi.arr[18]
			} else if (length < this.arrs[19]) {
				return diansha.shuzi.arr[19]
			} else if (length < this.arrs[20]) {
				return diansha.shuzi.arr[20]
			} else if (length < this.arrs[21]) {
				return diansha.shuzi.arr[21]
			} else if (length < this.arrs[22]) {
				return diansha.shuzi.arr[22]
			} else if (length < this.arrs[23]) {
				return diansha.shuzi.arr[234]
			} else if (length < this.arrs[24]) {
				return diansha.shuzi.arr[24]
			} else if (length < this.arrs[25]) {
				return diansha.shuzi.arr[25]
			} else if (length < this.arrs[26]) {
				return diansha.shuzi.arr[26]
			} else if (length < this.arrs[27]) {
				return diansha.shuzi.arr[27]
			} else if (length < this.arrs[28]) {
				return diansha.shuzi.arr[28]
			} else if (length < this.arrs[29]) {
				return diansha.shuzi.arr[29]
			} else if (length < this.arrs[30]) {
				return diansha.shuzi.arr[30]
			} else if (length < this.arrs[31]) {
				return diansha.shuzi.arr[31]
			}





		}


		/**
		 * 根据单位判断得出最后的数字
		 */
		checknum(n: number, danwei: string): number {
			let s = n + "";
			let b = "";
			if (s.indexOf(".") != -1) {
				let spre = s.split(".")[0]
				n = +spre;
				b = s.split(".")[1]

			}
			let index;
			shuzi.arr.forEach((v, i) => {
				if (danwei == v) {
					index = i;
					return;
				}
			})
			let sss = Number((n / Math.pow(1000, index)))
			if (danwei == "n") {
				sss = +Number((sss + ".") + b).toFixed(3)
			}
			return sss


		}


		/**
		 * 四则运算基础方法1
		 */
		private sizeyunsuan(one: shuzi) {
			this.newdanwei = diansha.shuzi.arr.indexOf(one.danwei)
			this.olddanwei = diansha.shuzi.arr.indexOf(this.danwei)
			this.oldi = this.num * Math.pow(1000, this.olddanwei);//最小单位数字this
			this.newi = one.num * Math.pow(1000, this.newdanwei);//最小单位数字one
		}
		/**
		 * 四则运算基础方法2
		 */
		private sizeyunsuan2() {

			let dw = this.checklength(this.he);
			let um = this.checknum(this.he, dw)
			return shuzi_factory.getinstance().get(um, dw);
		}
		/**
		 * 加
		 */
		public add(one: shuzi): shuzi {
			this.sizeyunsuan(one);
			this.he = this.oldi + this.newi;
			return this.sizeyunsuan2();


		}
		/**
		* 减
		*/
		public jian(one: shuzi): shuzi {
			this.sizeyunsuan(one);
			this.he = this.oldi - this.newi;
			return this.sizeyunsuan2();


		}
		/**
		 *乘
		 */
		public cheng(one: shuzi): shuzi {
			this.sizeyunsuan(one);
			this.he = this.oldi * this.newi;
			return this.sizeyunsuan2();


		}
		/**
		 *除(结果始终是n系列)
		 */
		public chu(one: shuzi): shuzi {
			this.sizeyunsuan(one);
			this.he = this.oldi / this.newi;
			return shuzi_factory.getinstance().get(this.he, "n");

		}
		/**
		 * 第一个和第二个数比较大小
		 * true:大于等于
		 */
		public dayudengyu(one: shuzi): boolean {
			this.newdanwei = diansha.shuzi.arr.indexOf(one.danwei)
			this.olddanwei = diansha.shuzi.arr.indexOf(this.danwei)
			this.oldi = this.num * Math.pow(1000, this.olddanwei);//最小单位数字this
			this.newi = one.num * Math.pow(1000, this.newdanwei);//最小单位数字one
			this.he = this.oldi - this.newi;
			// console.log("比较-------" + this.he)
			this.he = Number(this.he.toFixed(5))

			if (this.he > 0) {
				return true;
			} else {
				return false;
			}

		}
	}
}
